﻿Imports Data


Public Class PagoEmpleado
    Private Enum Estados
        Pagado = 1
        No_Pagado = 2
    End Enum



    Private Sub PagoEmpleado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Me.CargarCBXEmpleado()
            Me.CargarCBXEstado()
            Me.CargarCBXMes()
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub


    Private Function CargarCBXMes() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Meses _
                                                       Select New AbstractObject With {.ID = x.id, _
                                                                                  .Nombre = x.nombre}).ToList


                Dim manager As BindingManagerBase = Me.cbxMes.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxMes.DisplayMember = "Nombre"
                Me.cbxMes.ValueMember = "ID"
                Me.cbxMes.DataSource = lista
                Me.cbxMes.Enabled = True
                manager.ResumeBinding()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXEmpleado() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Empleados _
                                                       Select New AbstractObject With {.ID = x.id, _
                                                                                  .Nombre = String.Format("{0} {1} {2}", x.nombre, x.apellido_paterno, x.apellido_materno)}).ToList


                Dim manager As BindingManagerBase = Me.cbxEmpleado.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxEmpleado.DisplayMember = "Nombre"
                Me.cbxEmpleado.ValueMember = "ID"
                Me.cbxEmpleado.DataSource = lista
                Me.cbxEmpleado.Enabled = True
                manager.ResumeBinding()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Function CargarCBXEstado() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Estado _
                                                       Select New AbstractObject With {.ID = x.id, _
                                                                                  .Nombre = x.nombre}).ToList


                Dim manager As BindingManagerBase = Me.cbxEstado.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxEstado.DisplayMember = "Nombre"
                Me.cbxEstado.ValueMember = "ID"
                Me.cbxEstado.DataSource = lista
                Me.cbxEstado.Enabled = True
                manager.ResumeBinding()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Try
            Dim ingreso As New IngresoPago
            ingreso.ShowDialog()

        Catch ex As Exception

        End Try
    End Sub

   

    Private Sub btnResfrescar_Click(sender As System.Object, e As System.EventArgs) Handles btnResfrescar.Click
        Try
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub

    Private Function CargarLV() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim mes As Integer = Me.cbxMes.SelectedValue
                Dim empleado As Integer = Me.cbxEmpleado.SelectedValue
                Dim estado As Integer = Me.cbxEstado.SelectedValue

                Me.lvPagos.Items.Clear()

                Dim lista As List(Of Pago_Empleados) = (From x In db.Pago_Empleados _
                                              Where x.id_empleado = empleado And _
                                              x.id_estado = estado And _
                                              x.id_mes = mes _
                                              Select x).ToList


                For Each item In lista
                    Me.lvPagos.Items.Add(New ListViewItem(New String() {item.Empleados.nombre, _
                                                                       item.Empleados.apellido_paterno, _
                                                                       item.Meses.nombre, _
                                                                       item.Modo_Pago.nombre, _
                                                                       item.Forma_Pago.nombre, _
                                                                       item.monto}) With {.Name = item.id})
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub ModificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Try
            Dim id_pago As Integer = Me.lvPagos.SelectedItems(0).Name
            Dim m_ingresoPago As New IngresoPago(IngresoPago.Accion.Modificar, id_pago)
            m_ingresoPago.ShowDialog()
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            If (Me.Eliminar(Me.lvPagos.SelectedItems(0).Name)) Then
                MsgBox("Eliminado con exito")
                Me.CargarLV()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function Eliminar(ByVal id_pago As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim pago As Data.Pago_Empleados = db.Pago_Empleados.Where(Function(p) p.id = id_pago).Select(Function(p) p).FirstOrDefault
                db.Pago_Empleados.DeleteOnSubmit(pago)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub CancelarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CancelarToolStripMenuItem.Click
        Try
            If (Cancelar_Deuda(Me.lvPagos.SelectedItems(0).Name)) Then
                MsgBox("Deuda Cancelada")
                Me.CargarLV()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function Cancelar_Deuda(ByVal id_pago As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim pago As Data.Pago_Empleados = db.Pago_Empleados.Where(Function(p) p.id = id_pago).Select(Function(p) p).FirstOrDefault
                pago.id_estado = Estados.Pagado
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class